package com.brackets.stringparser;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Dmitry
 */
public class StringParser {
    private char[] chars;
    private char[] whiteSpace = new char[]{' ', '\n', '\r', '\t'};
    private int currentPosition = 0;

    public StringParser(String value) {
        this(value.toCharArray(), false);
    }

    public StringParser(char[] value, boolean copy) {
        if (value == null) {
            throw new IllegalArgumentException("value should not be null");
        }

        if (!copy) {
            chars = value;
        } else {
            chars = new char[value.length];
            for (int i = 0; i < value.length; i++) {
                chars[i] = value[i];
            }
        }
    }

    /**
     * Calculate start end of each line in text
     */
    public StringLayout getStringLayout() {
        List<Integer> linesStarts = new ArrayList<>(10);
        linesStarts.add(0);
        for (int i = 0; i < chars.length; i++) {
            if (chars[i] == '\n' && (i + 1) <= chars.length) {
                linesStarts.add(i + 1);
            }
        }
        int[] indexes = new int[linesStarts.size()];
        for (int i = 0; i < indexes.length; i++) {
            indexes[i] = linesStarts.get(i);
        }
        return new StringLayout(indexes);
    }

    public int getCurrentPosition() {
        return currentPosition;
    }

    public StringParser setEmptySymbols(char... c) {
        whiteSpace = c;
        return this;
    }

    public boolean eof() {
        return currentPosition >= chars.length;
    }

    public String readUntilWhitespace(){
        StringBuilder sb=new StringBuilder();
        while(true){
            char c=peek();
            if(c==0||isWhitespace(c)){
                break;
            }

            next();
            sb.append(sb);
        }

        return sb.toString();
    }
    
    private boolean isWhitespace(char c){
        for(int i=0;i<whiteSpace.length;i++){
            if(c==whiteSpace[i]){
                return true;
            }
        }
        return false;
    }
    
    /**
     * Get the next character,
     */
    public char get() {
        if (eof()) {
            return (char) 0;
        }

        char c = chars[currentPosition];
        currentPosition++;
        return c;
    }

    public StringParser next() {
        currentPosition++;
        return this;
    }

    /**
     * Get the next character, but without incrementing internal counter
     */
    public char peek() {
        if (eof()) {
            return (char) 0;
        }

        char c = chars[currentPosition];
        return c;
    }

    public char peek(int offset) {
        if ((currentPosition + offset) >= chars.length) {
            return (char) 0;
        }

        char c = chars[currentPosition + offset];
        return c;
    }

    public StringParser seek(int pos) {
        if (pos < 0) {
            throw new IllegalArgumentException("Cannot seek to the negative value(" + pos + ")");
        }
        if (pos >= chars.length) {
            throw new IllegalArgumentException("Cannot seek to the position " + pos + ", because buffer length=" + chars.length);
        }

        currentPosition = pos;
        return this;
    }

    public StringParser skip(int size) {
        for (int i = 0; i < size; i++) {
            get();
        }
        return this;
    }

    public StringParser skipWhitespace() {
        loop:
        while (!eof()) {
            char c = peek();
            for (int i = 0; i < whiteSpace.length; i++) {
                if (c == whiteSpace[i]) {
                    next();
                    break;
                } else {
                    break loop;
                }
            }
        }

        return this;
    }
    
    public String getString(int length){
        StringBuilder sb=new StringBuilder(length);
        for(int i=0;i<length;i++){
            char c=get();
            if(eof()){
                sb.append("<EOF>");
                return sb.toString();
            }
            sb.append(c);
        }
        return sb.toString();
    }
}
